<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .active {
        color: red;
      }
    </style>
  </head>

  <body>
    <div id="app"></div>

    <template id="my-app">
      <!-- 
      对象语法：{active: Boolean}
        - active：类名，加或不加引号都会被当成字符串
        - Boolean：布尔值
    -->
      <div :class="{active: isActive}">{{message}}</div>
      <button @click="toggle">切换类</button>

      <!-- 也可以有多个键值对 -->
      <div :class="{active: isActive, title: true}">哈哈哈</div>

      <!-- 默认的class和动态class结合 -->
      <div class="abc" :class="{active: isActive, title: true}">哈哈哈</div>

      <!-- 将对象放到一个单数的属性中 -->
      <div :class="classObj">哈哈哈</div>

      <!-- 将对象放到methods(computed)方法中 -->
      <div :class="getClassObj()">哈哈哈</div>
    </template>

    <script src="../js/Vue.js"></script>
    <script>
      const app = {
        template: '#my-app',
        data() {
          return {
            message: 'Hello World',
            isActive: true,
            classObj: {
              active: true,
              title: true
            }
          }
        },
        methods: {
          toggle() {
            this.isActive = !this.isActive
          },
          getClassObj() {
            return {
              active: true,
              title: true
            }
          }
        }
      }

      Vue.createApp(app).mount('#app')
    </script>
  </body>
</html>
